﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Modles.DTO;
using Modles.Models;

namespace BilbilDAL
{
    public class ImageDAL
    {
        public List<Image> GetImages()
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                return com.Images.ToList();
            }
        }
        public List<Image> GetImagesByIdOrDescription(int Id, string Description)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                IQueryable<Image> images = from image in com.Images select new Image() 
                { 
                    Id = image.Id,
                    Address = image.Address,
                    Description = image.Description
                };
                if(Id > 0)
                {
                    images = images.Where(x => x.Id == Id);
                }
                if(Description != null)
                {
                    images = images.Where(x => x.Description.Contains(Description));
                }
                return images.ToList();
            }
        }
        public bool AddImage(Image_DTO image)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                com.Images.Add(new Image() { Address = image.Address, Description = image.Description});
                return com.SaveChanges() > 0;
            }
        }
        public bool DeleteImage(int Id)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                Image image = com.Images.Find(Id);
                if (image != null)
                {
                    com.Images.Remove(image);
                }
                return com.SaveChanges() > 0;
            }
        }
        public bool UpdataImage(Image image)
        {
            using (VideoPlatformDbContext com = new VideoPlatformDbContext())
            {
                Image images = com.Images.Find(image.Id);
                if (image != null)
                {
                    images.Address = image.Address;
                    images.Description = image.Description;
                    com.Images.Update(images);
                }
                return com.SaveChanges() > 0;
            }
        }
    }
}
